/**
 * 
 */
package com.subway.babynote.syncdata;

import org.json.*;

import java.util.*;
/**
 * @author zfp
 *
 */
public abstract class ClientDataRecordBase {
	public static class ClientDataRecord_TYPE
	{
		public static final int contactRecord = 1;
		public static final int feedbackRecord = 2;
		public static final int feedRecord = 3;
		public static final int food_defaultRecord = 4;
		public static final int food_propRecord = 5;
		public static final int food_uxRecord = 6;
		public static final int foodsRecord = 7;
		public static final int hwAvgRecord = 8;
		public static final int hwdefaultRecord = 9;
		public static final int hwRecord = 10;
		public static final int hwUserRecord = 11;
		public static final int last_avgRecord = 12;
		public static final int loadinfoRecord = 13;
		public static final int tipRecord = 14;
		public static final int tsType = 15;
		public static final int tsType2 = 16;
		public static final int user_auth_info = 17;
		public static final int usersRecord = 18;
		public static final int uxRecord = 19;
		public static final int visitorRecord = 20;
		public static final int weibocredential = 21;
		public static final int weiboRecord = 22;
		public static final int returnValue = 23;
		public static final int ts = 24;
		public static final int list = 25;
		public static final int map = 26;
		public static final int max = 27;
	}
	protected int record_type = 0;
	protected abstract JSONObject to_JSON();
	protected abstract void init_type();
	public int get_type()
	{
		return this.record_type;
	}
	public JSONObject toJSON()
	{
		JSONObject jsono = new JSONObject();
		try
		{
			jsono.put(PostParam.ClientDataRecordBase.record_type, this.record_type);
			write_jsono(jsono, PostParam.ClientDataRecordBase.content, to_JSON());
			return jsono;
		}
		catch(JSONException jex)
		{
			return null;
		}
	}
	public static ClientDataRecordBase fromJSON(JSONObject jsono)
	{
		ClientDataRecordBase crb = null;
		int rt = read_int(jsono, PostParam.ClientDataRecordBase.record_type);
		JSONObject content = read_json(jsono, PostParam.ClientDataRecordBase.content);
		switch(rt)
		{
		case ClientDataRecord_TYPE.contactRecord:
			crb = new ClientDataRecord.contactRecord(content);
			break;
		case ClientDataRecord_TYPE.feedbackRecord:
			crb = new ClientDataRecord.feedbackRecord(content);
			break;
		case ClientDataRecord_TYPE.feedRecord:
			crb = new ClientDataRecord.feedRecord(content);
			break;
		case ClientDataRecord_TYPE.food_defaultRecord:
			crb = new ClientDataRecord.food_defaultRecord(content);
			break;
		case ClientDataRecord_TYPE.food_propRecord:
			crb = new ClientDataRecord.food_propRecord(content);
			break;
		case ClientDataRecord_TYPE.food_uxRecord:
			crb = new ClientDataRecord.food_uxRecord(content);
			break;
		case ClientDataRecord_TYPE.foodsRecord:
			crb = new ClientDataRecord.foodsRecord(content);
			break;
		case ClientDataRecord_TYPE.hwAvgRecord:
			crb = new ClientDataRecord.hwAvgRecord(content);
			break;
		case ClientDataRecord_TYPE.hwdefaultRecord:
			crb = new ClientDataRecord.hwdefaultRecord(content);
			break;
		
		case ClientDataRecord_TYPE.hwUserRecord:
			crb = new ClientDataRecord.hwUserRecord(content);
			break;
		case ClientDataRecord_TYPE.last_avgRecord:
			crb = new ClientDataRecord.last_avgRecord(content);
			break;
		case ClientDataRecord_TYPE.loadinfoRecord:
			crb = new ClientDataRecord.loadinfoRecord(content);
			break;
		case ClientDataRecord_TYPE.tipRecord:
			crb = new ClientDataRecord.tipRecord(content);
			break;
		case ClientDataRecord_TYPE.tsType:
			crb = new ClientDataRecord.tsType(content);
			break;
		
		case ClientDataRecord_TYPE.user_auth_info:
			crb = new ClientDataRecord.user_auth_info(content);
			break;
		case ClientDataRecord_TYPE.usersRecord:
			crb = new ClientDataRecord.usersRecord(content);
			break;
		case ClientDataRecord_TYPE.uxRecord:
			crb = new ClientDataRecord.uxRecord(content);
			break;
		case ClientDataRecord_TYPE.ts:
			crb = new ClientDataRecord.ts(content);
			break;
		case ClientDataRecord_TYPE.weibocredential:
			crb = new ClientDataRecord.weibocredential(content);
			break;
		case ClientDataRecord_TYPE.weiboRecord:
			crb = new ClientDataRecord.weiboRecord(content);
			break;
		case ClientDataRecord_TYPE.returnValue:
			crb = new ClientDataRecord.returnValue(content);
			break;
		case ClientDataRecord_TYPE.list:
			crb = new ClientDataRecord.list_ClientDataRecord(content);
			break;
		case ClientDataRecord_TYPE.map:
			crb = new ClientDataRecord.map_ClientDataRecord(content);
			break;
			
		}
		return crb;
	}
	public static JSONArray toJSONArray(List<ClientDataRecordBase> l)
	{
		Iterator<ClientDataRecordBase> it = l.iterator();
		JSONArray jsona = new JSONArray();
		while(it.hasNext())
		{
			ClientDataRecordBase cdrb = it.next();
			if(cdrb != null)
			{
				jsona.put(cdrb.toJSON());
			}
		}
		return jsona;
	}
	
	public static List<ClientDataRecordBase> fromJSONArray(JSONArray jsona)
	{
		List<ClientDataRecordBase> l = new ArrayList<ClientDataRecordBase>();
		try
		{
			int len = jsona.length();
			for(int i=0;i<len;i++)
			{
				JSONObject jsono = jsona.getJSONObject(i);
				ClientDataRecordBase crb = ClientDataRecordBase.fromJSON(jsono);
				l.add(crb);
			}
		}
		catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
		return l;
	}
	
	protected static void write_jsono(JSONObject o, String key, JSONObject value)
	{
		if(value != null)
			try {
				o.put(key, value);
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}	
	}
	protected static void write_jsona(JSONObject o, String key, JSONArray value)
	{
		if(value != null)
			try {
				o.put(key, value);
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}	
	}
	
	protected static void write_str(JSONObject o, String key, String value)
	{
		if(value != null)
			try {
				o.put(key, value);
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}		
	}
	
	protected static String read_str(JSONObject i, String key)
	{
		String a = "";
		
			try {
				if(i.has(key))
					a = i.getString(key);
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		return a;	
	}
	
	public static int read_int(JSONObject i, String key)
	{
		int a = -1;
		
			try {
				if(i.has(key))
					a = i.getInt(key);
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		return a;
	}
	protected static float read_float(JSONObject i, String key)
	{
		float f = 0;
		
			try {
				if(i.has(key))
					f = (float) i.getDouble(key);
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		return f;
	}
	
	public static JSONObject read_json(JSONObject i, String key)
	{
		JSONObject jsono = null;
		
			try {
				if(i.has(key))
					jsono = i.getJSONObject(key);
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		return jsono;
	}
	protected static JSONArray read_jsonA(JSONObject i, String key)
	{
		JSONArray jsona = null;
		
			try {
				if(i.has(key))
					jsona = i.getJSONArray(key);
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		return jsona;
	}	
	
}
